<template>
  <div class="login-right">
    <div class="register">
      <router-link to="/login/">
        <svg-icon icon-class="back" />
      </router-link>
    </div>
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" label-position="left">
      <div :class="[isRight? 'error-msg transparent': 'error-msg']">
        两次密码输入不一致，请重新输入
      </div>
      <h3 class="title">忘记密码</h3>
      <p class="prompt">请输入账号和验证码！</p>
      <el-form-item :class="[isRight? '': 'error-border']" prop="username">
        <el-input v-model="loginForm.username" name="username" type="text" placeholder="请输入账号"/>
        <span class="send-code" @click="showPwd">
          发送验证码
        </span>
      </el-form-item>
      <el-form-item :class="[isRight? '': 'error-border']" prop="code">
        <el-input
          v-model="loginForm.code"
          type="text"
          name="code"
          placeholder="请填写验证码"
          @keyup.enter.native="handleLogin"/>
      </el-form-item>
      <el-form-item class="btn">
        <el-button :loading="loading" type="primary" style="width:100%;" @click="handleGoReset">
          下一步
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { isvalidUsername, IsValidatCode } from '@/utils/validate'
import { sendEmail, sendSms } from '@/api/message'

export default {
  name: 'ForgetPwd',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!isvalidUsername(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    const validateCode = (rule, value, callback) => {
      if (!IsValidatCode(value)) {
        callback(new Error('请输入正确的验证码'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        code: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }]
      },
      isRight: true,
      loading: false,
      pwdType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('Login', this.loginForm).then(() => {
            this.loading = false
            this.$router.push({ path: this.redirect || '/' })
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 进行验证验证码
    handleGoReset() {
      this.$refs.loginForm.validate(valid => {
        console.log(valid)
        if (valid && IsValidatCode(this.loginForm.code)) {
          const account = this.loginForm.username
          const code = this.loginForm.code
          this.$store.dispatch('VerificationToCode', { account: account, msg_type: 4, code: code }).then(() => {
            this.loading = false
            this.$router.push({ path: '/login/resetPwd' })
          }).catch(() => {
            this.loading = false
          })
        }
      })
    },
    // 发送验证码
    showPwd() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          if (this.loginForm.username.indexOf('@') != -1) {
            const email = this.loginForm.username
            sendEmail(email, 4).then(response => {
              if (response.status === 200) {
                this.serviceInfo = response.data
              } else {
                console.log(response.msg)
              }
            })
          } else {
            const mobile = this.loginForm.username
            sendSms(mobile, 4).then(response => {
              if (response.status === 200) {
                this.serviceInfo = response.data
              } else {
                console.log(response.msg)
              }
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>

  $pink: #Faa39e;
  $light_orange: rgba(254, 241, 240, 1);
  $red: rgba(244, 41, 53, 1);
  $blue: #2390FF;
  .send-code {
    position: absolute;
    right: 10px;
    top: 0;
    font-size: 16px;
    color: $blue;
    cursor: pointer;
    user-select: none;
  }

  .btn {
    margin-top: 44px;
  }

  .error-msg {
    width: 320px;
    height: 28px;
    background: $light_orange;
    border-radius: 2px;
    border: 1px solid $pink;
    text-align: center;
    line-height: 28px;
    font-size: 12px;
    font-family: MicrosoftYaHei;
    color: $red;
  }

  .transparent {
    opacity: 0;
  }

  .error-border {
    border: 1px solid $pink;
  }
</style>

